package com.yzy.fly.type;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

public enum JJFieldType {
    BYTE("byte", "java.lang.byte",JDBCType.VARCHAR.getType()),
    CHAR("char", "java.lang.char",JDBCType.VARCHAR.getType()),
    STRING("String", "java.lang.String",JDBCType.VARCHAR.getType()),
    INT("int", "I",JDBCType.INT.getType()),
    INTEGER("Integer", "java.lang.Integer",JDBCType.INTEGER.getType()),
    LONG_BASIC("long", "java.lang.long",JDBCType.INTEGER.getType()),
    LONG_PACKAGE("Long", "java.lang.Long",JDBCType.INTEGER.getType()),
    DOUBLE_BASIC("double", "java.lang.double",JDBCType.DOUBLE.getType()),
    DOUBLE_PACKAGE("Double", "java.lang.Double",JDBCType.DOUBLE.getType()),
    FLOAT_BASIC("float", "java.lang.float",JDBCType.DOUBLE.getType()),
    FLOAT_PACKAGE("Float", "java.lang.Float",JDBCType.DOUBLE.getType()),
    BOOLEAN_BASIC("boolean", "java.lang.boolean",JDBCType.TINYINT.getType()),
    BOOLEAN_PACKAGE("Boolean", "java.lang.Boolean",JDBCType.TINYINT.getType()),
//    _STRING("String", "java.lang.String"),
    BIGDECIMAL("BigDecimal", "java.math.BigDecimal",JDBCType.DOUBLE.getType()),
    JSON("JSONObject", JSONObject.class.getName(),JDBCType.JSON.getType()),
    JSONOBJECT("JSONObject", JSONObject.class.getName(),JDBCType.JSON.getType()),
    JSONARRAY("JSONArray", JSONArray.class.getName(),JDBCType.JSON.getType());

    @Setter
    @Getter
    private String type;
    @Setter
    @Getter
    private String javaType;
    @Setter
    @Getter
    private String jdbcType;

    JJFieldType(String type, String javaType, String jdbcType) {
        this.type = type;
        this.javaType = javaType;
        this.jdbcType = jdbcType;
    }

    public static JJFieldType type(String type) {
        if (StringUtils.isBlank(type)) {
            return null;
        }
        JJFieldType fieldType = null;
        JJFieldType[] values = JJFieldType.values();
        try {
            for (int i = 0; i < values.length; i++) {
                JJFieldType value = values[i];
                if (value.getType().equals(type)) {
                    fieldType = value;
                    break;
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            fieldType = null;
        }
        return fieldType;
    }

    public static JJFieldType javaType(String javaType) {
        if (StringUtils.isBlank(javaType)) {
            return null;
        }
        JJFieldType fieldType = null;
        JJFieldType[] values = JJFieldType.values();
        try {
            for (int i = 0; i < values.length; i++) {
                JJFieldType value = values[i];
                if (value.getJavaType().equals(javaType)) {
                    fieldType = value;
                    break;
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            fieldType = null;
        }
        return fieldType;
    }

    public static JJFieldType jdbcType(String jdbcType) {
        if (StringUtils.isBlank(jdbcType)) {
            return null;
        }
        JJFieldType fieldType = null;
        JJFieldType[] values = JJFieldType.values();
        try {
            for (int i = 0; i < values.length; i++) {
                JJFieldType value = values[i];
                if (value.getJdbcType().equals(jdbcType)) {
                    fieldType = value;
                    break;
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            fieldType = null;
        }
        return fieldType;
    }
}
